package com.hepinghospital.user.ui.activity.home;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.util.ImageLoaderHelper;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.Doctor;
import com.rest.response.TeamDetailResponse;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.DpUtil;
import jz.joyoung.robot.util.UtilString;


/**
 * 团队问诊-团队详情
 */

public class TeamDetailActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_level)
    TextView tv_level;
    @BindView(R.id.tv_hospital)
    TextView tv_hospital;
    @BindView(R.id.tv_skill)
    TextView tv_skill;
    @BindView(R.id.img_head)
    ImageView img_head;
    @BindView(R.id.tv_star)
    TextView tv_star;
    @BindView(R.id.btn_more)
    TextView btn_more;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    String id;
    DoctorAdapter adapter;
    List<Doctor> doctorList = new ArrayList<>();


    @Override
    protected void initViews() {
        tvTitle.setText("");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new DoctorAdapter();
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = DpUtil.dip2px(TeamDetailActivity.this, 16);
            }
        });

        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setOnRefreshListener(refreshlayout -> initData());
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {
        });
    }

    @Override
    protected void initData() {
        id = getIntent().getStringExtra("id");
        getTeamDetail();
    }


    @OnClick(R.id.btn_more)
    void btn_more() {
        if (tv_skill.getMaxLines() == 3) {
            btn_more.setText("点击收起>");
            tv_skill.setMaxLines(100);
        } else {
            btn_more.setText("查看全部 >");
            tv_skill.setMaxLines(3);
        }
    }


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_team_detail;
    }

    private void getTeamDetail() {
        RestProxy.getInstance().getTeamDetail(id, new Observer<TeamDetailResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(TeamDetailResponse teamDetailResponse) {
                tvTitle.setText(teamDetailResponse.name);
                tv_name.setText(teamDetailResponse.leaderDoctorName);
                tv_level.setText("负责人:" + teamDetailResponse.leaderDoctorName + " " + teamDetailResponse.leaderDoctorTitle);
                tv_hospital.setText(teamDetailResponse.hospitalName);
                tv_skill.setText("团队介绍:" + teamDetailResponse.description);
                tv_star.setText(teamDetailResponse.score);
                ImageLoaderHelper.getInstance().GlideCircleImageLoader(TeamDetailActivity.this, teamDetailResponse.thumbnailUrl, img_head);
                doctorList.clear();
                doctorList.addAll(teamDetailResponse.detailDtoList);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(TeamDetailActivity.this, e);
                refreshLayout.finishRefresh(false);
            }

            @Override
            public void onComplete() {
                refreshLayout.finishRefresh(true);
            }
        });
    }

    class DoctorAdapter extends RecyclerView.Adapter<DoctorAdapter.Holder> {

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from(TeamDetailActivity.this).inflate(R.layout.item_team_doctor, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener(v -> {
                Bundle bundle = new Bundle();
                bundle.putSerializable("id", doctorList.get(position).id);
                startActivity(DoctorDetailActivity.class, bundle);
            });
            holder.tv_level.setText(doctorList.get(position).jobTitleName);
            holder.tv_skill.setText("擅长:" + doctorList.get(position).docGoodAtNames);
            holder.tv_star.setText(doctorList.get(position).score);
            holder.tv_name.setText(doctorList.get(position).docName);
            ImageLoaderHelper.getInstance().GlideCircleImageLoader(TeamDetailActivity.this, doctorList.get(position).thumbnailUrl, holder.img_head);
            if (doctorList.get(position).isPrescription == 1) {
                holder.tv_recipel.setText("处方");
                holder.tv_recipel.setTextColor(Color.parseColor("#FFAB26"));
            } else {
                holder.tv_recipel.setText("非处方");
                holder.tv_recipel.setTextColor(Color.parseColor("#ffd5d5d5"));
            }
            if (!UtilString.isEmpty(doctorList.get(position).num)) {
                holder.tv_turnNum.setVisibility(View.VISIBLE);
                holder.tv_turnNum.setText(doctorList.get(position).num + "");
            } else {
                holder.tv_turnNum.setVisibility(View.GONE);
            }
            if (!UtilString.isEmpty(doctorList.get(position).deptName)) {
                holder.tv_deptName.setText(doctorList.get(position).deptName);
            } else {
                holder.tv_deptName.setVisibility(View.GONE);
            }
        }

        @Override
        public int getItemCount() {
            return doctorList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_level, tv_recipel, tv_deptName, tv_skill, tv_star, tv_turnNum;
            ImageView img_head;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_level = itemView.findViewById(R.id.tv_level);
                tv_recipel = itemView.findViewById(R.id.tv_recipel);
                tv_deptName = itemView.findViewById(R.id.tv_deptName);
                tv_skill = itemView.findViewById(R.id.tv_skill);
                tv_star = itemView.findViewById(R.id.tv_star);
                img_head = itemView.findViewById(R.id.img_head);
                tv_turnNum = itemView.findViewById(R.id.tv_turn_num);
            }
        }
    }
}
